DP A
Going from start to finish
At each point, there are two options to proceed one or two steps forward, with different costs
The problem of finding the minimum cost to reach the goal
Let each point be the domain of definition and the minimum cost to get there be the value DP
DP_A
code:python
def solve(N, heights):
costs1 = abs(heights1 - heights0) for i in range(2, N):
)
Because of the way it is written, "choose the smaller of the current value and the new value," it is initialized with a sufficiently large value INF.
Actually, for this problem, the second min always has the smallest new value and is always updated first, so we can eliminate this min and not initialize the cost
I needed to reserve one larger to avoid out-of-range accesses because i needed to run just before the goal, but it references two ahead of it.
The equivalent is handled in the form of "pre-calculating 0s and 1s" and "starting the loop from 2" in the DP to be collected.
Personally, I feel like I need to twist my head a bit
code:python
def solve(N, heights):
for i in range(N - 1):
costsi + abs(heightsi + 1 - heightsi)) costsi + abs(heightsi + 2 - heightsi)) Written by [memory recursion
this is an honest
Which values have been calculated is pushed to the memoizing section, and humans don't care.
It must be possible to identify if it has been calculated or not, so it is initialized with an impossible value (None) as a cost.
code:python
def solve(N, heights):
costs1 = abs(heights1 - heights0) def get_cost(i):
c = min(
get_cost(i - 2) + abs(heightsi - heightsi - 2), get_cost(i - 1) + abs(heightsi - heightsi - 1), )
return c
return get_cost(N - 1)
---
This page is auto-translated from /nishio/DP A. If you looks something interesting but the auto-translated English is not good enough to understand it, feel free to let me know at @nishio_en. I'm very happy to spread my thought to non-Japanese readers.